package easy.罗马数字转整数;

import java.util.HashMap;
import java.util.Map;

/**
 * @author CodeX
 * @since 2021-09-24 10:19:48
 */
public class RomanToInt {

    private static final Map<Character, Integer> lmMap = new HashMap<>();

    static {
        lmMap.put('I', 1);
        lmMap.put('V', 5);
        lmMap.put('X', 10);
        lmMap.put('L', 50);
        lmMap.put('C', 100);
        lmMap.put('D', 500);
        lmMap.put('M', 1000);
    }


    public static int romanToInt(String s) {
        int result = 0;
        int n = s.length();
        for (int i = 0; i < n; i++) {
            Integer value = lmMap.get(s.charAt(i));
            System.out.println("roman:" + s.charAt(i) + " value:" + value);

            if (i < n - 1 && value < lmMap.get(s.charAt(i + 1))) {
                result -= value;
            } else {
                result += value;
            }
        }

        return result;
    }

}
